/**
 * @file   GetClientMethodListResponsePacket.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2013-05-14
 * @brief  DataBus Get Client Method List Response packet.
 *
 * Copyright (C) 2013 Djuro Drljaca <djurodrljaca@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DataBus/Common/GetClientMethodListResponsePacket.h>

bool DataBus::GetClientMethodListResponsePacket::create(const quint8 source,
                                                        const quint8 destination,
                                                        const quint8 packetId,
                                                        const QList<quint8> &methodIdList,
                                                        Packet *packet)
{
    // Check parameters
    if ((source == 0) ||
        (packet == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Get No of Items
    int noOfItems = methodIdList.size();

    if (noOfItems > 254)
    {
        // Error, too many method IDs in the list
        return false;
    }

    // Create Header
    packet->setSource(source);
    packet->setDestination(destination);
    packet->setPacketType(PacketType_GetClientMethodListResponse);
    packet->setPacketId(packetId);

    // Create Payload
    QByteArray payload;

    payload.append(static_cast<char>(noOfItems));

    for (int i = 0; i < noOfItems; i++)
    {
        payload.append(static_cast<quint8>(methodIdList.at(i)));
    }

    packet->setData(payload);

    // Success
    return true;
}

bool DataBus::GetClientMethodListResponsePacket::parse(const Packet &packet,
                                                       QList<quint8> *methodIdList)
{
    // Check parameters
    if ((packet.getSource() == 0) ||
        (packet.getPacketType() != PacketType_GetClientMethodListResponse) ||
        (methodIdList == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Get data
    QByteArray data = packet.getData();

    if (data.size() < 1)
    {
        // Error, at least No of items should be in the response
        return false;
    }

    // Get no of items
    int noOfItems = data.at(0);

    // Check if payload size is valid
    if (data.size() != (noOfItems + 1))
    {
        // Error, invalid no of items
        return false;
    }

    // Get Method ID list
    methodIdList->clear();

    for (int i = 0; i < noOfItems; i++)
    {
        // Get Method ID
        quint8 methodId = static_cast<quint8>(data.at(1 + i));

        if (methodIdList->indexOf(methodId) >= 0)
        {
            // Error, this Method ID is already in the list
            return false;
        }

        // Add Method ID to the list
        methodIdList->append(methodId);
    }

    // Success
    return true;
}
